﻿using Lonewolf.Entity;
using Lonewolf.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lonewolf.Service
{
    public partial class LoginInfoService
    {
        public async Task<bool> SaveAsync(LoginInfo model)
        {
            try
            {
                if (model == null)
                    return false;
                if (model.Id.IsNullOrEmpty())//新增
                {
                    model.LoginType = (int)LoginType.admin;
                    model.LoginKey = RandomHelper.GetRandom(8).DESEncrypt().Substring(0, 8);
                    model.LoginPwd = model.LoginPwd.DESEncrypt(model.LoginKey).MD5Encrypt();
                    model.LoginNumber = 0;
                    await InsertAsync(model);
                }
                else//修改
                {
                    model.LastChangeTime = DateTime.Now;
                    model.LoginPwd = model.LoginPwd.DESEncrypt(model.LoginKey).MD5Encrypt();
                    Update(model);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }
        public LoginInfo GetByAccount(string userId)
        {
            try
            {
                return Query().FirstOrDefault(a => a.UserId == userId);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
